<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class ShopHost extends Model
{
    use HasFactory;
    const REDIS_PREFIX="allenapi_shop_host_list_";
    protected $table = "shop_host_list";
    protected $guarded = [];
    public static function refreshData2Redis($shop_id,$host_id){
        if(empty($shop_id)){
            return null;
        }
        if(empty($host_id)){
            return null;
        }
        $me=self::where('origin_member_id',$host_id)->first();
        if(!empty($me)){
            if(empty($me->shop_id)){
                self::where('origin_member_id',$host_id)->update([
                    'shop_id'=>$shop_id
                ]);
            }
            $fields=[
                'shop_id'=>$shop_id,
                'origin_id'=>$me->origin_id,
                'origin_account'=>$me->origin_account,
                'origin_name'=>$me->origin_name,
                'origin_member_id'=>$me->origin_member_id,
            ];
            Redis::set(self::REDIS_PREFIX.$host_id,json_encode($fields));
        }else{
            $originManager=self::shopDB_queryLiveManagerFromMemberId($host_id);
            $originMember=self::shopDB_queryMemberFromMemberId($host_id);
            if(!empty($originManager)&&!empty($originMember)){
                $fields=[
                    'shop_id'=>$shop_id,
                    'origin_id'=>$originManager->id,//live_maneger.id
                    'origin_account'=>$originMember->username,//member.username
                    'origin_name'=>$originManager->user_name,//live_maneger.user_name
                    'origin_member_id'=>$originManager->member_id,
                ];
                $me=self::create($fields);
                Redis::SET(self::REDIS_PREFIX.$host_id,json_encode($fields));
            }
        }
        self::refreshPoints($host_id);
        return $me;
    }
    public static function newShopHost($data){
        $shop_id=$data['shop_id']??null;
        $host_id=$data['host_id']??null;
        if(empty($shop_id)){
            return null;
        }
        if(empty($host_id)){
            return null;
        }
        $me=Redis::get(self::REDIS_PREFIX.$host_id);
        if(!empty($me)){
            return json_decode($me);
        }
        $me=self::where('origin_member_id',$host_id)->first();
        if(!empty($me)){
            if(empty($me->shop_id)){
                self::where('origin_member_id',$host_id)->update([
                    'shop_id'=>$shop_id
                ]);
            }
            $fields=[
                'shop_id'=>$shop_id,
                'origin_id'=>$me->origin_id,
                'origin_account'=>$me->origin_account,
                'origin_name'=>$me->origin_name,
                'origin_member_id'=>$me->origin_member_id,
            ];
            Redis::set(self::REDIS_PREFIX.$host_id,json_encode($fields));
        }else{
            $originManager=self::shopDB_queryLiveManagerFromMemberId($host_id);
            $originMember=self::shopDB_queryMemberFromMemberId($host_id);
            if(!empty($originManager)&&!empty($originMember)){
                $fields=[
                    'shop_id'=>$shop_id,
                    'origin_id'=>$originManager->id,//live_maneger.id
                    'origin_account'=>$originMember->username,//member.username
                    'origin_name'=>$originManager->user_name,//live_maneger.user_name
                    'origin_member_id'=>$originManager->member_id,
                ];
                $me=self::create($fields);
                Redis::SET(self::REDIS_PREFIX.$host_id,json_encode($fields));
            }
        }
        return $me;
    }
    public static function setPoints($host_id,$oprate){
        $point=self::getPoints($host_id);
        if(empty($point))
        $point=0;
        $c=0;
        switch($oprate['flag']){
            case '+':{
                $c=$point+$oprate['value'];
                break;
            }
            case '-':{
                $c=$point-$oprate['value'];
                break;
            }
            default:{
                return null;
            }
        }
        $v=self::where('origin_member_id',$host_id)->update(['points'=>$c]);
        if(!empty($v)){
         Redis::SET(self::REDIS_PREFIX."{$host_id}_points",$c);
        }
        return [$point,$c];
    }
    public static function getPoints($host_id){
        $cache_key=self::REDIS_PREFIX."{$host_id}_points";
        $points=0;
        if(Redis::EXISTS($cache_key)&&!empty(Redis::EXISTS($cache_key))){
            $points=Redis::GET($cache_key);
        }else{
            if(self::where('origin_member_id',$host_id)->exists()){
                $points=self::where('origin_member_id',$host_id)->value('points');
            }else{
                $points=0;
            }
            Redis::SET($cache_key,$points);
        }
        return $points;
    }
    public static function decPoints($host_id,$point){
        $cache_key=self::REDIS_PREFIX."{$host_id}_points";
        Redis::DECRBY($cache_key,$point);
        return true;
    }
    public static function incPoints($host_id,$point){
        $cache_key=self::REDIS_PREFIX."{$host_id}_points";
        Redis::INCRBY($cache_key,$point);
        return true;
    }
    public static function refreshPoints($host_id){
        $points=self::getPoints($host_id);
        return self::where('origin_member_id',$host_id)->update([
            'points'=>$points
        ]);
    }
    private static function shopDB_queryFromId($id){
        return DB::connection('shop')->table('live_manager')->where('id',$id)->first();
    }
    private static function shopDB_queryLiveManagerFromMemberId($id){
        return DB::connection('shop')->table('live_manager')->where('member_id',$id)->first();
    }
    private static function shopDB_queryMemberFromMemberId($id){
        return DB::connection('shop')->table('member')->where('member_id',$id)->first();
    }
}
